package com.example.paizhaogou.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.paizhaogou.BitmapCache;
import com.example.paizhaogou.Entity.Item;
import com.example.paizhaogou.R;

import java.util.ArrayList;
import java.util.List;

public class ItemRecycleAdapter extends RecyclerView.Adapter<ItemRecycleAdapter.MyViewHolder> {
    private List<Item> items = new ArrayList<Item>();//数据
    private Context context;//上下文
    private OnItemClickListener mOnItemClickListener;//回调方法

    public ItemRecycleAdapter(List<Item> items, Context context) {
        this.items = items;
        this.context = context;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @NonNull
    @Override
    //返回一个自定义的ViewHolder
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }

    @Override
    //填充onCreateViewHolder方法返回holder中的控件
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Item item = items.get(position);
        holder.name.setText(item.getSkuName());
        holder.price.setText(item.getSkuPrice());
        holder.detailUrl.setText(item.getDetailUrl());
        loadCacheImage(item.getImageUrl(), holder.imageView);
//设置点击回调
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    private void loadCacheImage(String url, ImageView volley_image) {
        //创建网络请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(context);

        //创建请求
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());//带缓存
        //加载不到图片，加载失败，采用默认图
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(volley_image, R.mipmap.ic_launcher, R.mipmap.ic_launcher);

        imageLoader.get(url, imageListener);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView name;
        public TextView price;
        public TextView detailUrl;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.image);
            name=itemView.findViewById(R.id .item_name);
            price=itemView.findViewById(R.id.item_price);
            detailUrl=itemView.findViewById(R.id.item_detailUri);
        }
    }
}
